var express = require("express");
var router = express.Router();
const { getList, getDetail, newBlog, updateBlog, deleteBlog } = require("../controller/blog");
// const loginCheck = require("../utils/loginCheck");
const loginCheck = require("../middleware/loginCheck");

//获取博客列表
router.get("/list", (req, res, next) => {
    const author = req.query.author || "";
    const keyword = req.query.keyword || "";
    getList(author, keyword).then((data) => {
        res.json({
            status: 200,
            data
        })       
    })
})
//获取博客文章
router.get("/detail", (req, res, next) => {
    const id = req.query.id || "";
    getDetail(id).then((data) => {
        res.json({
            status: 200,
            data
        })
    })
})
//新建博客
router.post("/new", loginCheck, (req, res, next) => {
    newBlog(req.body).then((data) => {
        if (data.affectedRows >= 1) {
            res.json({ msg: "新建博客成功" })
        } else {
            res.json({ msg: "新建博客失败" })
        }
    })
})
//更新博客
router.post("/update", loginCheck, (req, res, next) => {
    updateBlog(req.body).then((data) => {
        if (data.affectedRows >= 1) {
            res.json({ msg: "更新博客成功" })
        } else {
            res.json({ msg: "更新博客失败" })
        }
    })
})
//删除博客
router.post("/delete", loginCheck, (req, res, next) => {
    deleteBlog(req.body).then((data) => {
        if (data.affectedRows >= 1) {
            res.json({ msg: "删除博客成功" })
        } else {
            res.json({ msg: "删除博客失败" })
        }
    })
})

module.exports = router;